/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ru.rantipin.GUI;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import ru.rantipin.persistence.PersistenceHelper;

/**
 *
 * @author rantipin
 */
public class AdhesionDataBasePanel extends JPanel{

    public AdhesionDataBasePanel(ResearchDataBasePanel panel) {
        initComponentsLayout();
        panelWithSample = panel;
    }

    private void initComponentsLayout() {
        GridBagLayout gridbag = new GridBagLayout();
        setLayout(gridbag);

        JLabel researchNameLabel = new JLabel("Name:");
        gridbag.setConstraints(researchNameLabel,
                new GridBagConstraints(
                    0, 1,
                    GridBagConstraints.REMAINDER, 1,
                    0.0, 0.0,
                    GridBagConstraints.LINE_START, GridBagConstraints.NONE,
                    new Insets(0, 10, 0, 10),
                    0, 0
                ));
        add(researchNameLabel);

        gridbag.setConstraints(getNameText(),
                new GridBagConstraints(
                    0, 2,
                    GridBagConstraints.REMAINDER, 1,
                    0.0, 0.0,
                    GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL,
                    new Insets(5, 10, 0, 10),
                    0, 0
                ));
        add(getNameText());

        JLabel dateLabel = new JLabel("Black percent:");
        gridbag.setConstraints(dateLabel,
                new GridBagConstraints(
                    0, 3,
                    1, 1,
                    0.0, 0.0,
                    GridBagConstraints.LINE_START, GridBagConstraints.NONE,
                    new Insets(10, 10, 0, 10),
                    0, 0
                ));
        add(dateLabel);

        gridbag.setConstraints(getBlackPercent(),
                new GridBagConstraints(
                    1, 3,
                    1, 1,
                    0.0, 0.0,
                    GridBagConstraints.LINE_END, GridBagConstraints.NONE,
                    new Insets(10, 10, 0, 10),
                    0, 0
                ));
        add(getBlackPercent());

        JLabel descriptionLabel = new JLabel("Description:");
        gridbag.setConstraints(descriptionLabel,
                new GridBagConstraints(
                    0, 4,
                    1, 1,
                    0.0, 0.0,
                    GridBagConstraints.LINE_START, GridBagConstraints.NONE,
                    new Insets(10, 10, 0, 10),
                    0, 0
                ));
        add(descriptionLabel);

        JScrollPane descriptionArea = new JScrollPane(getDescriptionTextArea());
        descriptionArea.setMinimumSize(new Dimension(0, 60));
        descriptionArea.setPreferredSize(new Dimension(0, 60));
        gridbag.setConstraints(descriptionArea,
                new GridBagConstraints(
                    0, 5,
                    GridBagConstraints.REMAINDER, 1,
                    1.0, 1.0,
                    GridBagConstraints.LINE_START, GridBagConstraints.BOTH,
                    new Insets(5, 10, 0, 10),
                    0, 0
                ));
        add(descriptionArea);

        gridbag.setConstraints(getCleanButton(),
                new GridBagConstraints(
                    0, 6,
                    1, 1,
                    1.0, 0.0,
                    GridBagConstraints.LINE_END, GridBagConstraints.NONE,
                    new Insets(10, 10, 0, 0),
                    0, 0
                ));
        add(getCleanButton());

        gridbag.setConstraints(getSaveButton(),
                new GridBagConstraints(
                    1, 6,
                    1, 1,
                    0.0, 0.0,
                    GridBagConstraints.LINE_END, GridBagConstraints.NONE,
                    new Insets(10, 5, 0, 10),
                    0, 0
                ));
        add(getSaveButton());
    }

    public JTextArea getDescriptionTextArea() {
        if (discriptionTextArea == null) {
            discriptionTextArea = new JTextArea();
            discriptionTextArea.setLineWrap(true);
            discriptionTextArea.setWrapStyleWord(true);
        }
        return discriptionTextArea;
    }

    public JTextField getBlackPercent() {
        if (blackPercentText == null) {
            blackPercentText = new JTextField(5);
        }
        return blackPercentText;
    }

    public JTextField getNameText() {
        if (nameText == null) {
            nameText = new JTextField();
        }
        return nameText;
    }

    public JButton getSaveButton() {
        if (saveButton == null) {
            saveButton = new JButton("Save");
            saveButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    try {
                        PersistenceHelper.getInstance().saveAdchesion(
                                panelWithSample.getSelectedSample(),
                                getNameText().getText(),
                                Double.valueOf(getBlackPercent().getText()),
                                getDescriptionTextArea().getText());
                        cleanPanel();
                    } catch (Exception ex) {
                        Logger.getLogger(AdhesionDataBasePanel.class.getName()).log(Level.SEVERE, null, ex);
                        JOptionPane.showMessageDialog(AdhesionDataBasePanel.this, ex.getMessage(),
                            "Save result error", JOptionPane.ERROR_MESSAGE);
                    }
                }
            });
        }
        return saveButton;
    }

    public JButton getCleanButton() {
        if (cleanButton == null) {
            cleanButton = new JButton("Clean");
            cleanButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    cleanPanel();
                }
            });
        }
        return cleanButton;
    }

    private void cleanPanel() {
        getBlackPercent().setText("");
        getDescriptionTextArea().setText("");
        getNameText().setText("");
    }

    private ResearchDataBasePanel panelWithSample = null;

    private JTextArea discriptionTextArea = null;
    private JTextField blackPercentText = null;
    private JTextField nameText = null;
    private JButton saveButton = null;
    private JButton cleanButton = null;
}
